package com.study.d018tasks;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
*@description
*@author xtayhame
*date 2020年3月16日
*version
*since v1.0
*/
public class ScheduledServiceMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();

		Button startButton = new Button("开始");
		Button cancelButton = new Button("取消");
		Button restartButton = new Button("重启");
		Button resetButton = new Button("重置");

		ProgressBar  progressBar = new ProgressBar(0.0);
		Label statLabel = new Label("stat");
		Label valueLabel = new Label("value");
		Label titleLable = new Label("title");
		Label messageLable = new Label("message");


		HBox hBox = new HBox(15);
		hBox.getChildren().addAll(startButton,cancelButton,restartButton,resetButton, progressBar,statLabel,valueLabel,titleLable,messageLable);

		AnchorPane.setTopAnchor(hBox, 30.0);
		AnchorPane.setLeftAnchor(hBox, 30.0);

		anchorPane.getChildren().add(hBox);
		stage.setScene(new Scene(anchorPane, 600, 400));
		stage.show();


		MyScheduledService myScheduledService = new MyScheduledService();
		myScheduledService.setDelay(Duration.seconds(3));
		myScheduledService.setPeriod(Duration.minutes(2));

		//设置最大失败次数
		myScheduledService.setMaximumFailureCount(3);
		//设置每次递增时间
		myScheduledService.setMaximumCumulativePeriod(Duration.minutes(5));

		//开始线程
		startButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				myScheduledService.start();
			}
		});

		//取消
		cancelButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				myScheduledService.cancel();
			}
		});

		//重启
		restartButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				myScheduledService.restart();
			}
		});

		//重置
		resetButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//重置时，将数字重新置为0
				myScheduledService.setSum(0);
				myScheduledService.reset();
			}
		});





		//监听进度
		myScheduledService.progressProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				valueLabel.setText(newValue.toString());
				//注意：这里绝对不能使用intValue()进行转换 ,如果转换为int类，则滚动条的进度无法显示，这里需要的是double类型的参数
				//progressBar.setProgress(newValue.doubleValue());
			}
		});

		//监听值
		myScheduledService.valueProperty().addListener(new ChangeListener<Number>() {

			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				if(newValue !=null){
					valueLabel.setText(newValue.toString());
				}
			}
		});


		//监听状态
		myScheduledService.stateProperty().addListener(new ChangeListener<State>() {
			@Override
			public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {

				if(newValue !=null){
					statLabel.setText("当前状态：" +newValue.name());
					System.out.println("线程状态:" + newValue.name());
				}

				/**
				 * State.SCHEDULED 准备
				 * State.RUNNING   运行
				 * State.SUCCEEDED 成功
				 */
			}
		});

		//监听消息
		myScheduledService.messageProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				if(newValue !=null){
					messageLable.setText(newValue);
					System.out.println("消息：" + newValue);
				}

			}
		});

		//标题监听
		myScheduledService.titleProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				if(valueLabel !=null){
					titleLable.setText(newValue);
					System.out.println("标题：" + newValue);
				}

			}
		});


		//捕获异常
		myScheduledService.exceptionProperty().addListener(new ChangeListener<Throwable>() {

			@Override
			public void changed(ObservableValue<? extends Throwable> observable, Throwable oldValue,
					Throwable newValue) {
				System.out.println(newValue);
			}
		});


	}

}

class MyScheduledService extends ScheduledService<Number>{
	private int sum=0;

	public void setSum(int sum) {
		this.sum = sum;
	}

	@Override
	protected void executeTask(Task<Number> task) {
		super.executeTask(task);
		System.out.println("executeTask="+task);
	}

	@Override
	protected Task<Number> createTask() {
		Task<Number> task = new Task<Number>() {
			@Override
			protected void updateValue(Number value) {
				super.updateValue(value);
				if(value.intValue() ==15){
					//取消任务
					MyScheduledService.this.cancel();
				}

			}

			@Override
			protected Number call() throws Exception {
				for(int i=0;i<20;i++){
					sum =sum + 1;
					Thread.sleep(2000);
					System.out.println("sum="+sum);
					this.updateValue(sum);
				}
				return sum;
			}
		};

		return task;
	}

}
